package edu.bbu.bs.backend.service.beans;

import java.util.List;
import java.util.logging.Logger;

import javax.ejb.DependsOn;
import javax.ejb.EJB;
import javax.ejb.Stateless;

import junit.framework.Test;
import edu.bbu.bs.backend.model.Author;
import edu.bbu.bs.backend.repository.AuthorRepository;
import edu.bbu.bs.backend.repository.exceptions.RepositoryException;
import edu.bbu.bs.backend.service.AuthorService;
import edu.bbu.bs.backend.service.exceptions.BsServiceException;

@Stateless(name = "AuthorService", mappedName = "ejb/AuthorService")
@DependsOn({ "AuthorRepository" })
public class AuthorServiceBean implements AuthorService {

	@EJB
	private AuthorRepository repo;
	
	private static Logger logger = Logger.getLogger(AuthorServiceBean.class.getName());
	
	@Override
	public Author getAuthor(Long id) {
		try {
			logger.info("INFO B!");
			return repo.getById(id);
		} catch (final RepositoryException ex) {
			throw new BsServiceException("Get author failed", ex);
		}
	}

	@Override
	public Author insertAuthor(Author author) {
		try {
			return repo.save(author);
		} catch (final RepositoryException ex) {
			throw new BsServiceException("Insert author failed", ex);
		}
	}

	@Override
	public Author updateAuthor(Author author) {
		try {
			return repo.merge(author);
		} catch (final RepositoryException ex) {
			throw new BsServiceException("Update author failed", ex);
		}
	}

	@Override
	public void deleteAuthor(Author author) {
		try {
			repo.delete(author);
		} catch (final RepositoryException ex) {
			throw new BsServiceException("Delete author failed", ex);
		}
	}

	@Override
	public void deleteById(Long id) {
		try {
			repo.delete(new Author(id, "", ""));
		} catch (final RepositoryException ex) {
			throw new BsServiceException("Delete author failed", ex);
		}
	}

	@Override
	public List<Author> getAllAuthor() {
		try {
			return repo.getAll();
		} catch (final RepositoryException ex) {
			throw new BsServiceException("Get authors failed", ex);
		}
	}

}
